/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Petani;

import Daerah.Daerah;
import Daerah.DaerahKontrol;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author gatz
 */
public class FormPetani extends javax.swing.JFrame {

    /**
     * Creates new form KategoriForm2
     */
    public FormPetani() {
        initComponents();
        NamaPetaniText.requestFocus();
        SimpanButton.setEnabled(false);
        tampilPetani();
        tampilDaerahCombo();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        NamaPetaniText = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        IdPetaniText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        DaerahComboBox = new javax.swing.JComboBox();
        SimpanButton = new javax.swing.JButton();
        HapusButton = new javax.swing.JButton();
        KeluarButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TabelPetani = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 102, 102));

        jLabel1.setBackground(new java.awt.Color(255, 102, 0));
        jLabel1.setFont(new java.awt.Font("Verdana", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Daftar Petani");

        jLabel4.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel4.setText("Nama Petani");

        NamaPetaniText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NamaPetaniTextActionPerformed(evt);
            }
        });
        NamaPetaniText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                NamaPetaniTextKeyReleased(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel6.setText("ID Petani");

        jLabel5.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel5.setText("Daerah");

        DaerahComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DaerahComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(NamaPetaniText, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(IdPetaniText))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(DaerahComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DaerahComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(IdPetaniText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(NamaPetaniText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        SimpanButton.setText("Simpan");
        SimpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SimpanButtonActionPerformed(evt);
            }
        });

        HapusButton.setText("Hapus");
        HapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HapusButtonActionPerformed(evt);
            }
        });

        KeluarButton.setText("Keluar");
        KeluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeluarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(SimpanButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(HapusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(135, 135, 135)
                        .addComponent(KeluarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(32, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(120, 120, 120))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SimpanButton)
                    .addComponent(HapusButton)
                    .addComponent(KeluarButton))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(0, 102, 102));

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Daftar Petani");

        TabelPetani.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Id Petani", "Nama Petani", "Daerah"
            }
        ));
        TabelPetani.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TabelPetaniMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TabelPetani);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void KeluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KeluarButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_KeluarButtonActionPerformed

    private void NamaPetaniTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NamaPetaniTextActionPerformed
        // TODO add your handling code here:
        SimpanButton.setEnabled(true);
    }//GEN-LAST:event_NamaPetaniTextActionPerformed

    private void NamaPetaniTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NamaPetaniTextKeyReleased
        // TODO add your handling code here:
//        try {
//            // TODO add your handling code here:
//            String kodeKategori = KodeKategoriText.getText().trim();
//            String namaKat = KategoriKontrol.getKoneksiKategori().cariKategori(kodeKategori);
//            NamaKategoriText.setText(namaKat);
//        } catch (SQLException ex) {
//            Logger.getLogger(KategoriForm.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_NamaPetaniTextKeyReleased

    private void SimpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SimpanButtonActionPerformed
        // TODO add your handling code here:
        String id = IdPetaniText.getText();
        String nama = NamaPetaniText.getText();
        String kode = DaerahComboBox.getItemAt(DaerahComboBox.getSelectedIndex()).toString();
        String kddaerah = kode.substring(0, 5);
        String nmdaerah = kode.substring(5);
        Petani petani = new Petani();
        Daerah dr = new Daerah();
        petani.setIdPetani(id);
        petani.setNamaPetani(nama);
        dr.setIdDaerah(kddaerah);
        dr.setNamaDaerah(nmdaerah);
        petani.setDaerah(dr);
        PetaniKontrol petaniKontrol;
        try {
            petaniKontrol = PetaniKontrol.getKoneksiPetani();
            if (SimpanButton.getText().equalsIgnoreCase("Simpan")) {
                // ini pake method biasa
                petaniKontrol.tambahPetani(petani);

                // ini pake Colable Stored Procedure
//                bakulKontrol.tambahBakulCall(bakul);
                JOptionPane.showMessageDialog(this, "Data dengan nama " + nama + " berhasil disimpan ");
                IdPetaniText.setText(" ");
                NamaPetaniText.setText(" ");

                tampilPetani();
            } else {
                petaniKontrol.updatePetani(petani);
                JOptionPane.showMessageDialog(this, "Data Berhasil diupdate = " + nama);
                IdPetaniText.setText(" ");
                NamaPetaniText.setText(" ");

                tampilPetani();
            }
        } catch (SQLException ex) {
            Logger.getLogger(FormPetani.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_SimpanButtonActionPerformed

    private void HapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HapusButtonActionPerformed
        Petani petani = new Petani();
        petani.setIdPetani(IdPetaniText.getText());
        petani.setNamaPetani(NamaPetaniText.getText());

        PetaniKontrol petaniKontrol;
        try {
            petaniKontrol = PetaniKontrol.getKoneksiPetani();

            petaniKontrol.deletePetani(petani);
            JOptionPane.showMessageDialog(null, "Data petani dengan id " + IdPetaniText.getText()
                    + "\nberhasil didelete !!");
            IdPetaniText.setText("");
            NamaPetaniText.setText("");

        } catch (SQLException ex) {
            Logger.getLogger(FormPetani.class.getName()).log(Level.SEVERE, null, ex);
        }
        tampilPetani();
    }//GEN-LAST:event_HapusButtonActionPerformed

    private void TabelPetaniMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TabelPetaniMouseClicked
        // TODO add your handling code here:
        int baris = TabelPetani.getSelectedRow();
        String kode = TabelPetani.getValueAt(baris, 0).toString();
        String nama = TabelPetani.getValueAt(baris, 1).toString();
        String daerah = TabelPetani.getValueAt(baris, 2).toString();

        IdPetaniText.setText(kode);
        NamaPetaniText.setText(nama);
        DaerahComboBox.setSelectedItem(daerah);

        SimpanButton.setEnabled(true);
        SimpanButton.setText("Update");

    }//GEN-LAST:event_TabelPetaniMouseClicked

    private void DaerahComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DaerahComboBoxActionPerformed
        // TODO add your handling code here:
        if(!SimpanButton.getText().equals("Update")){
        try {
            String fix;
            String kode = DaerahComboBox.getItemAt(DaerahComboBox.getSelectedIndex()).toString();
            String kd = kode.substring(0, 5);
            long tmbh = Long.parseLong(PetaniKontrol.getKoneksiPetani().cariIdPetani(kd));
            String tmbh1 = String.valueOf(tmbh);
            if (tmbh < 10) {
                String a = "00";
                fix = a + tmbh1;
                String jd = kd + fix;
                IdPetaniText.setText(jd);
            } else if (tmbh < 100 && tmbh > 9) {
                String b = "0";
                fix = b + tmbh1;
                String jd = kd + fix;
                IdPetaniText.setText(jd);
            } else if (tmbh > 99 && tmbh < 1000) {
                fix = String.valueOf(tmbh);
                String jd = kd + fix;
                IdPetaniText.setText(jd);
            } else {
                JOptionPane.showMessageDialog(this, "TIDAK DAPAT DITAMBAH LAGI.");
                IdPetaniText.setText("");
            }
        } catch (SQLException ex) {
            Logger.getLogger(FormPetani.class.getName()).log(Level.SEVERE, null, ex);
        }}else {
            
        }
    }//GEN-LAST:event_DaerahComboBoxActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormPetani.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormPetani.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormPetani.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormPetani.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormPetani().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox DaerahComboBox;
    private javax.swing.JButton HapusButton;
    private javax.swing.JTextField IdPetaniText;
    private javax.swing.JButton KeluarButton;
    private javax.swing.JTextField NamaPetaniText;
    private javax.swing.JButton SimpanButton;
    private javax.swing.JTable TabelPetani;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void tampilPetani() {
        try {
            List<Petani> daftarPetani = PetaniKontrol.getKoneksiPetani().lihatSeluruhPetani();

            PetaniTabelModel model = new PetaniTabelModel(daftarPetani);
            TabelPetani.setModel(model);

        } catch (SQLException ex) {
            Logger.getLogger(FormPetani.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void tampilDaerahCombo() {
        try {
            DaerahComboBox.removeAllItems();
            List<Daerah> daftardaerah = DaerahKontrol.getKoneksiDaerah().lihatSeluruhDaerah();
            for (int i = 0; i < daftardaerah.size(); i++) {
                Daerah daerah = daftardaerah.get(i);
                DaerahComboBox.addItem(daerah.getIdDaerah() + " " + daerah.getNamaDaerah());
            }
        } catch (SQLException ex) {
            Logger.getLogger(FormPetani.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
